﻿package{
	import flash.display.Sprite;
	import flash.display.Graphics ;
	import flash.display.Shape;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.media.SoundChannel;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.events.Event;
	import flash.geom.Point;
	
	public class ChineseCharacter{
		private var strokes:Array;//存放笔画信息 //ab//1a
		private var beziers:Array;//存放贝塞尔曲线//#1,1,2,3,4,5#55,43,24,115,43;123,54,223,123
		//private var fillDirection:Array;//存放填充方向//0101//1230//0
		//private var points:Array;//起始点结束点和拐点信息
		//private var audioFile:String;//声音
		private var strokeShape:Array;//存放每一个笔画的shape
		private var fullShape:Bitmap;
		private var filterShapeRight:Bitmap;
		private var filterShapeWrong:Bitmap;
		private var strokeShape2:Array;
		public function ChineseCharacter(strokes:Array,beziers:Array/*,fillDirection:Array,audioFile:String*/){
			
			//初始化各种参数
			//cwp=new Cwp();
			
				
				
			//this.points=[];
			this.strokeShape=new Array();
			this.strokeShape2=new Array();
			this.strokes=new Array();
			//this.fillDirection=new Array();
			this.beziers=new Array();
			//this.audioFile="";
			
			
			/*setStrokes(cwp.getStrokes());
			setBeziers(cwp.getBeziers());
			setFillDirection(cwp.getFillDirection());
			setAudioFile(cwp.getAudioFile());
			*/
			try{
			setStrokes(strokes);
			setBeziers(beziers);
			//setFillDirection(fillDirection);
			//setAudioFile(audioFile);
			}catch(errObject:Error){
				trace(errObject.message);
				}
			
			}
		public function getFullShape():Bitmap{
			return this.fullShape;
			}
		public function setStrokes(a:Array):void{
			this.strokes=a;
			//this.strokes=["横","竖"];
			}
		public function setBeziers(a:Array):void{
			this.beziers=a;
			//this.beziers=[[[44,174,37,174,25,168,15,161,21,160,109,151,184,138,212,131,228,145,234,154,225,156,144,152,66,170,61,171,53,172,46,173],[[21,160],[225,156]]],[[119,72,141,78,139,86,137,90,135,118,135,132,135,166,135,208,134,238,132,278,125,286,118,293,118,240,120,163,119,119,117,84,110,80,105,75,108,73,111,70],[[108,73],[125,286]]]];
			drawShape();
			}
		public function drawShape(){
			try{
					var f:Shape=new Shape();
					var filter:Shape=new Shape();
					var filter2:Shape=new Shape();
					for(var i:int=1;i<beziers.length;i++){
						//trace("hah");
						var t1:Shape=new Shape();
						var aa:Array=beziers[i][0];//用来画图的贝塞尔曲线信息
						var sts:Array=new Array();
						for(var j:int=0;j<aa.length;j++){
							//trace(a.join("//"));
							var a:Array=aa[j];//每一段的笔画数据
							var t:Shape=new Shape();
							
							t.graphics.lineStyle(1,0x888880);//边框宽度
							t.graphics.beginFill(0x888880);//开始填充，颜色黑色
							t1.graphics.lineStyle(1,0x000000);//边框宽度
							t1.graphics.beginFill(0x000000);//开始填充，颜色黑色
							f.graphics.lineStyle(1,0x000000);//边框宽度
							f.graphics.beginFill(0x000000);//开始填充，颜色黑色
							filter.graphics.lineStyle(1,0x00ff00);//边框宽度
							filter.graphics.beginFill(0x00ff00);//开始填充，颜色黑色
							filter2.graphics.lineStyle(1,0xff0000);//边框宽度
							filter2.graphics.beginFill(0xff0000);//开始填充，颜色黑色
							if(beziers[0]==2){//二次曲线画图
								t.graphics.moveTo(a[0],(265-a[1]));//移动到初始点
								for(var j1:int=2;j1<a.length-2;j1=j1+4){
					
									t.graphics.curveTo(a[j1],(265-a[j1+1]),a[j1+2],(265-a[j1+3]));					
		
								}
								t.graphics.curveTo(a[j1],(265-a[j1+1]),a[0],(265-a[1]));
							}
							else if(beziers[0]==3){//三次曲线画图
								t.graphics.moveTo(a[0],a[1]);//移动到初始位置
								t1.graphics.moveTo(a[0],a[1]);//移动到初始位置
								f.graphics.moveTo(a[0],a[1]);//移动到初始位置
								filter.graphics.moveTo(a[0],a[1]);//移动到初始位置
								filter2.graphics.moveTo(a[0],a[1]);//移动到初始位置
								for(j1=0;j1<=a.length-8;j1=j1+6){
							//var s:Point = cubic([new Point(a[0],a[1]),new Point(a[2],a[3]),new Point(a[4],a[5]),new Point(a[6],a[7])], 0); 
							//t.graphics.curveTo(a[j1],(265-a[j1+1]),a[j1+2],(265-a[j1+3]));					
									bezier(t,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[j1+6],a[j1+7])],20);//调用3茨曲线函数画图
									bezier(t1,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[j1+6],a[j1+7])],20);//调用3茨曲线函数画图
									bezier(f,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[j1+6],a[j1+7])],20);//调用3茨曲线函数画图
									bezier(filter,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[j1+6],a[j1+7])],20);//调用3茨曲线函数画图
									bezier(filter2,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[j1+6],a[j1+7])],20);//调用3茨曲线函数画图
		
								}
								//bezier(t,[new Point(a[j1],a[j1+1]),new Point(a[j1+2],a[j1+3]),new Point(a[j1+4],a[j1+5]),new Point(a[0],a[1])],20);
							}
							t.graphics.endFill();
							var bmp:Bitmap;
							var bmpd:BitmapData=new BitmapData(350,350,true,0x00000000);
							bmpd.draw(t,new Matrix(1,0,0,1,0,0));
							bmp=new Bitmap(bmpd);//用画出的图形生成位图
							//strokeShape.push(bmp);
							//bmp.scaleX=0.65;
							//bmp.scaleY=0.65;
							sts.push(bmp);//存储位图
							
						}
						var cv:Array=new Array();
						t1.graphics.endFill();
							var bmp3:Bitmap;
							var bmpd3:BitmapData=new BitmapData(350,350,true,0x00000000);
							bmpd3.draw(t1,new Matrix(1,0,0,1,0,0));
							bmp3=new Bitmap(bmpd3);//用画出的图形生成位图
							cv.push(bmp3);
							strokeShape2.push(cv);//存储所有位图
						strokeShape.push(sts);//存储所有位图
					}
					
				f.graphics.endFill();
				var bmpd2:BitmapData=new BitmapData(350,350,true,0x00000000);
				bmpd2.draw(f,new Matrix(1,0,0,1,0,0));
				this.fullShape=new Bitmap(bmpd2);//用画出的图形生成位图
				var bmpd4:BitmapData=new BitmapData(350,350,true,0x00000000);
				bmpd4.draw(filter,new Matrix(1,0,0,1,0,0));
				this.filterShapeRight=new Bitmap(bmpd4);
				
				var bmpd5:BitmapData=new BitmapData(350,350,true,0x00000000);
				bmpd5.draw(filter2,new Matrix(1,0,0,1,0,0));
				this.filterShapeWrong=new Bitmap(bmpd5);
				}catch(errObject:Error){
				trace(errObject.message);
				}
			}
		/*public function setFillDirection(a:Array):void{
			this.fillDirection=a;
			//trace(a);
			//this.fillDirection=[0,2];
			}
		public function setAudioFile(s:String):void{
			this.audioFile=s;
			//this.audioFile="shi.mp3";
			}*/
		public function getStrokeShapeForWrite():Array{
			return this.strokeShape2;
			}
		public function getCharacterFilterRight():Bitmap{
			return this.filterShapeRight;
			}
		public function getCharacterFilterWrong():Bitmap{
			return this.filterShapeWrong;
			}
		public function getStrokeShapeForFill():Array{
			//trace(strokeShape[i].length+"//123123");
			return this.strokeShape;
			}
		public function getStrokes():Array{
			return this.strokes;
			}
		public function getStartPoint(i:int):Array{
			var s:Array=new Array();
			for(var j:int=0;j<beziers[i+1][0].length;j++){
				//trace(j);
				//trace(beziers[i+1][0][j][0]);
				//s.push(new Point(beziers[i+1][0][j][0],(265-beziers[i+1][0][j][1])));
				
				s.push(new Point(beziers[i+1][0][j][0],beziers[i+1][0][j][1]));//返回起始点
				//trace(s[j]);
				}
				//trace (s);
			return s;
			
			}
		public function getStartAndEndPoints():Array{
			var s:Array=new Array();
			
			for(var k=0;k<this.beziers.length-1;k++){
				
				var spoint=new Point(beziers[k+1][1][0][0],beziers[k+1][1][0][1]);
				var epoint=new Point(beziers[k+1][1][1][0],beziers[k+1][1][1][1]);
				var a=new Array();
				a.push(spoint);
				a.push(epoint);
				s.push(a);
				
			}
			
				
			return s;
			
			}
		/*public function getFillDirection():Array{
			return this.fillDirection;
			}
			*/
		public function bezier(t:Shape,p:Array,segments:Number) {   

    
		var dt:Number = 1/segments;   //精度
		var s:Point = cubic(p, 0);  
		
		//t.graphics.moveTo(s.x, s.y);      
		for (var i:Number=1; i<=segments; i++) {        
			s = cubic(p, i*dt);        
			t.graphics.lineTo(s.x, s.y);    
			}
			}
		public function cubic(p:Array, t:Number):Point {    // Takes 4 Points  
			if (t < 0 || t > 1 || p.length != 4) return null;     
			var ax:Number, bx:Number, cx:Number; 
			cx = 3*(p[1].x - p[0].x);   
			bx = 3*(p[2].x - p[1].x) - cx;   
			ax = p[3].x - p[0].x - bx - cx;      
			var ay:Number, by:Number, cy:Number;  
			cy = 3*(p[1].y - p[0].y);  
			by = 3*(p[2].y - p[1].y) - cy;   
			ay = p[3].y - p[0].y - by - cy;      
			var t2:Number = t*t;    var t3:Number = t*t2;    
			return new Point(ax*t3 + bx*t2 + cx*t + p[0].x,ay*t3 + by*t2 + cy*t + p[0].y);
			}
		}
	}